package com.leyou.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.service.SpecGroupService;
import com.leyou.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecParamService specParamService;
    @Autowired
    private SpecGroupService specGroupService;

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity addSpecParam(@RequestBody SpecParamDTO specParamDTO) {

        specParamService.addSpecParam(specParamDTO);
       return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    /**
     * 修改规格参数
     */

    @PutMapping("/param")
    public ResponseEntity updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {



        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }
    /**
     * 修改规格组
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {

        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();

    }

    /**
     * 根据分类id查询规格组的集合
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> getSpecGroupDTOSByCategoryId(@RequestParam("id")Long id) {
        return ResponseEntity.ok(specGroupService.getSpecGroupDTOSByCategoryId(id));
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> getSpecGroupDTOSAndParamsByCategoryId(@RequestParam("id")Long cid) {
        return ResponseEntity.ok(specGroupService.getSpecGroupDTOSAndParamsByCategoryId(cid));
    }


    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> getSpecParamDTOSByParams(
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching) {
        return ResponseEntity.ok(specParamService.getSpecParamDTOSByParams(cid,groupId,searching));
    }
}
